package cn.com.thingo.hyintelligent.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.th.opensdk.openapi.AirConditionInfo;
import com.th.opensdk.openapi.RemoterInfo;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.com.thingo.hyintelligent.R;

public class AirConditionListAdapter extends RecyclerView.Adapter<AirConditionListAdapter.ViewHolder> {
    private List<RemoterInfo> list;
    private List<AirConditionInfo> airConditionInfos;
    private Context context;
    private OnItemClickListener listener;
    public static final int AIRCONDITION_CUSTOM = 1;
    public static final int AIRCONDITION_IMPORT = 2;


    public AirConditionListAdapter(List<RemoterInfo> list, List<AirConditionInfo> airConditionInfos, Context context) {
        this.list = list;
        this.airConditionInfos = airConditionInfos;
        this.context = context;
    }

    @Override
    public int getItemViewType(int position) {
        if (position < list.size()) {
            return AIRCONDITION_CUSTOM;
        } else {
            return AIRCONDITION_IMPORT;
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_air_condition_list, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") final int position) {
        int itemViewType = getItemViewType(position);
        if (itemViewType == AIRCONDITION_CUSTOM) {
            RemoterInfo remoterInfo = list.get(position);
            holder.tvName.setText(remoterInfo.getRemoterName());
            if (listener != null) {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        listener.onItemClick(position, AIRCONDITION_CUSTOM);
                    }
                });
            }
        } else {
            AirConditionInfo airConditionInfo = airConditionInfos.get(position - list.size());
            holder.tvName.setText(airConditionInfo.getDeviceName());
            if (listener!=null){
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        listener.onItemClick(position-list.size(),AIRCONDITION_IMPORT);
                    }
                });
            }
        }

    }

    @Override
    public int getItemCount() {
        return list.size() + airConditionInfos.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_air_condition_name)
        TextView tvName;
        @BindView(R.id.item_air_condition_type)
        TextView tvType;

        ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public void setListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public interface OnItemClickListener {
        void onItemClick(int position, int type);
    }
}
